package com.easy.cloud.web.module.certification.biz.service.impl;

import cn.hutool.core.util.StrUtil;
import com.easy.cloud.web.component.core.exception.BusinessException;
import com.easy.cloud.web.component.core.response.HttpResult;
import com.easy.cloud.web.module.certification.api.enums.CompanyCertificationClientEnum;
import com.easy.cloud.web.module.certification.api.enums.PersoanlCertificationClientEnum;
import com.easy.cloud.web.module.certification.biz.service.ICertificationService;
import com.easy.cloud.web.module.certification.biz.service.certification.company.CompanyCertificationProperties;
import com.easy.cloud.web.module.certification.biz.service.certification.company.ICompanyCertificationClient;
import com.easy.cloud.web.module.certification.biz.service.certification.company.entity.CompanyCertificationBody;
import com.easy.cloud.web.module.certification.biz.service.certification.personal.IPersonalCertificationClient;
import com.easy.cloud.web.module.certification.biz.service.certification.personal.PersonalCertificationProperties;
import com.easy.cloud.web.module.certification.biz.service.certification.personal.entity.PersonalCertificationBody;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Service;

import java.util.EnumMap;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;

/**
 * @author GR
 * @date 2024/2/19 19:31
 */
@Slf4j
@Service
public class CertificationServiceImpl implements ICertificationService, ApplicationContextAware {

    @Autowired
    private PersonalCertificationProperties personalCertificationProperties;

    @Autowired
    private CompanyCertificationProperties companyCertificationProperties;

    /**
     * 认证类型方式
     */
    private final Map<PersoanlCertificationClientEnum, IPersonalCertificationClient> personalCertificationClients = new EnumMap<>(PersoanlCertificationClientEnum.class);
    /**
     * 认证类型方式
     */
    private final Map<CompanyCertificationClientEnum, ICompanyCertificationClient> companyCertificationClients = new EnumMap<>(CompanyCertificationClientEnum.class);

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        applicationContext.getBeansOfType(IPersonalCertificationClient.class).values()
                .forEach(certificationClient -> personalCertificationClients.put(certificationClient.client(), certificationClient));
        applicationContext.getBeansOfType(ICompanyCertificationClient.class).values()
                .forEach(certificationClient -> companyCertificationClients.put(certificationClient.client(), certificationClient));
    }

    @Override
    public HttpResult<Object> personalCertification(PersonalCertificationBody personalCertificationBody) {
        // 通过渠道获取当前认证客户端
        if (Objects.isNull(personalCertificationProperties) || StrUtil.isBlank(personalCertificationProperties.getChannel())) {
            throw new BusinessException("当前个人认证信息未配置第三方认证信息");
        }

        // 获取配置渠道
        PersoanlCertificationClientEnum client = PersoanlCertificationClientEnum.valueOf(personalCertificationProperties.getChannel());
        // 获取渠道实现对象
        IPersonalCertificationClient certificationClient = Optional.ofNullable(personalCertificationClients.get(client))
                .orElseThrow(() -> new BusinessException(StrUtil.format("当前认证渠道不支持：%s", client)));
        return certificationClient.certification(personalCertificationBody);
    }

    @Override
    public HttpResult<Object> companyCertification(CompanyCertificationBody companyCertificationBody) {
        // 通过渠道获取当前认证客户端
        if (Objects.isNull(companyCertificationProperties) || StrUtil.isBlank(companyCertificationProperties.getChannel())) {
            throw new BusinessException("当前企业认证信息未配置第三方认证信息");
        }

        // 获取配置渠道
        CompanyCertificationClientEnum client = CompanyCertificationClientEnum.valueOf(companyCertificationProperties.getChannel());
        // 获取渠道实现对象
        ICompanyCertificationClient certificationClient = Optional.ofNullable(companyCertificationClients.get(client))
                .orElseThrow(() -> new BusinessException(StrUtil.format("当前企业认证渠道不支持：%s", client)));
        return certificationClient.certification(companyCertificationBody);
    }
}
